package net.flydy.ywxcx.wxcloudrun.controller;

import lombok.extern.slf4j.Slf4j;
import net.flydy.ywxcx.wxcloudrun.common.*;
import net.flydy.ywxcx.wxcloudrun.model.*;
import net.flydy.ywxcx.wxcloudrun.dto.*;
import net.flydy.ywxcx.wxcloudrun.service.impl.*;
import net.flydy.ywxcx.wxcloudrun.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.function.Supplier;

@Slf4j
public class BaseController {

    public boolean checkToken(HttpServletRequest httpReq, HttpServletResponse httpResp, BaseDTO dto) {
        return true;
    }

    public BaseResponse doService(Supplier<BaseResponse> supplier, BaseDTO baseDTO) {
        return doService(supplier, baseDTO, LoggerFactory.getLogger("apiLog"));
    }


    public BaseResponse doService(Supplier<BaseResponse> supplier, BaseDTO baseDTO, Logger recordLog) {
        baseDTO.setStartTime(new Date());
        BaseRequest baseRequest = baseDTO.getRequest();
        String reqNo = null;
        if (baseRequest != null) {
            reqNo = baseDTO.getRequest().getReqNo();
        }
        try {
            BaseResponse baseResponse = supplier.get();
            baseResponse.setReqNo(reqNo);
            if (StringUtils.isBlank(baseResponse.getMessage())) {
                String code = baseResponse.getCode();
                if (StringUtils.isBlank(code)) {
                    code = StatusEnum.SUCCESS.getCode();
                    baseResponse.setCode(code);
                }
                String message = BaseResponse.i18n.getString(code);
                baseResponse.setMessage(message);
            }
            baseDTO.setResponse(baseResponse);
            return baseResponse;
        } catch (Exception e) {
            if (e instanceof BaseException) {
                BaseResponse baseResponse = new BaseResponse();
                baseResponse.setReqNo(reqNo);
                BaseException be = (BaseException) e;
                baseResponse.setCode(be.getCode());
                baseResponse.setMessage(be.getMessage());
                baseDTO.setResponse(baseResponse);
                baseDTO.setOtherDesc(be.getOtherDesc());
                return baseResponse;
            } else {
                log.error("未知异常 reqNo="+reqNo, e);
                BaseResponse baseResponse = new BaseResponse(StatusEnum.FAIL);
                baseResponse.setReqNo(reqNo);
                baseDTO.setResponse(baseResponse);
                baseDTO.setExceptionInfo(e.getMessage());
                return baseResponse;
            }
        } finally {
            baseDTO.setEndTime(new Date());
            baseDTO.setCost(baseDTO.getEndTime().getTime() - baseDTO.getStartTime().getTime());
            //log dto info
            String apiLog = null;
            try {
                apiLog = MyJSONTools.toJsonStr(baseDTO);
            } catch (Exception e) {
                log.error("JSONTools.toJsonStr", e);
            }
            recordLog.info(apiLog);
        }
    }

}
